//
//  BaseAds.h
//  PaintWar
//
//  Created by Tai Le on 10/9/14.
//
//

#import <UIKit/UIKit.h>
#import "Global.h"
#import "AdsMixConfig.h"

//
typedef enum
{
    AdMixTypeWeight,//request iAd x times, if fail -> request GAD x times, if fail -> back to iAd
    AdMixTypePeriod,//request iAd in x secconds, if fail -> request GAD in x seconds, if fail -> back to iAd
}AdMixType;

//
@protocol BaseAdsDelegate;
@interface BaseAds : NSObject
{
    NSInteger _countRequestFail;
    NSTimeInterval _timeFirstRequest;
    NSTimeInterval _timeLastRequest;
    NSInteger _countSeconds;
    
    BOOL _isAdsVisible;
    ThreadStatus _threadStatus;
}

@property (nonatomic, weak)id<BaseAdsDelegate>delegate;
@property (nonatomic, assign) NSInteger numberOfAllowFails;//number of retry request, use for AdsMixTypeWeight
@property (nonatomic, assign) NSInteger period;//time to retry request, use for AdsMixTypePeriod
@property (nonatomic, assign) AdMixType adMixType;

- (BOOL)isOutOfFail;

/////////////////////////////////
//Only for subclass usages
/////////////////////////////////
- (void)raiseDidLoad:(id)ads;
- (void)raiseDidUnload:(id)ads;
- (void)raiseDidFail:(id)ads error:(NSError *)error;

/////////////////////////////////
//Subclasses must overide
/////////////////////////////////
- (id)init;//init ads and request ads in this function
- (void)cycleAds;
- (id)getAds;
- (void)releaseAds;
//only for Interstitial Ads
- (BOOL)showAdInterstitialInViewController:(UIViewController *)controller;
- (BOOL)showAdInterstitialInView:(UIView *)view;

@end

@protocol BaseAdsDelegate <NSObject>

//load content successfully, ready to show Ads
- (void)adsDidLoad:(BaseAds *)ad;

/*!
 * @method interstitialAdDidUnload:
 *
 * @discussion
 * When this method is invoked, if the application is using -presentInView:, the
 * content will be unloaded from the container shortly after this method is
 * called and no new content will be loaded. This may occur either as a result
 * of user actions or if the ad content has expired.
 *
 * In the case of an interstitial presented via -presentInView:, the layout of
 * the app should be updated to reflect that an ad is no longer visible. e.g.
 * by removing the view used for presentation and replacing it with another view.
 */
- (void)adsDidUnload:(BaseAds *)ad;

/*!
 * @method interstitialAd:didFailWithError:
 *
 * @discussion
 * Called when an error has occurred attempting to get ad content.
 *
 * @see ADError for a list of possible error codes.
 */
- (void)adsDidFail:(BaseAds *)ad withError:(NSError *)error;

@end
